﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Common {
	public class WebHelper {
		public static void AlertUtf8(string msg, int iType) {
            AlertUtf8(msg, iType,""); 
        }
		///<summary>向浏览器发送提示框,输出utf-8
		///<para>iType提示类别:</para> 
		///<para>   0 = alert(msg);window.close();</para> 
		///<para>   1 = alert(msg);self.location=sToUrl;</para>
		///<para>   2 = alert(msg);top.location=sToUrl;</para> 
		///<para>   3 = alert(msg);history.back();</para> 
		///<para>   4 = alert(msg); </para> 
		///<para>   5 = self.location=sToUrl;</para> 
		///<para>   6 = top.location=sToUrl;</para> 
		///<para>   7 = (直接输出script代码):msg </para> 
		///<para>   other = (直接输出htmp代码):msg </para> 
		/// </summary>
		/// <param name="msg">提示文字</param>
		/// <param name="iType">提示类别</param>
		/// <param name="sToUrl">跳舞转Url地址</param>
		public static void AlertUtf8(string msg, int iType, string sToUrl) {
			string str = "";
			if (iType < 5) {
				msg = FixTitle(msg);
				msg = ClearCrLf(msg);
			}

			if (iType == 0) {
				str = "<script>alert('" + msg + "');window.close();</script>";
			}
			else if (iType == 1) {
				str = "<script>alert('" + msg + "');self.location='" + sToUrl + "';</script>";
			}
			else if (iType == 2) {
				str = "<script>alert('" + msg + "');top.location='" + sToUrl + "';</script>";
			}
			else if (iType == 3) {
				str = "<script>alert('" + msg + "');history.back();</script>";
			}
			else if (iType == 4) {
				str = "<script>alert('" + msg + "');</script>";
			}
			else if (iType == 5) {
				str = "<script>self.location='" + sToUrl + "';</script>";
			}
			else if (iType == 6) {
				str = "<script>top.location='" + sToUrl + "';</script>";
			}
			else if (iType == 7) {
				str = "<script>" + msg + "</script>";
			}
			else {
				str = msg;
			}

			HttpResponse response = HttpContext.Current.Response;
			response.Clear();
			response.ContentType = "text/html";
			response.Charset = "utf-8";
			response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
			response.Expires = -1;
			response.AddHeader("pragma", "no-cache");
			response.AddHeader("cache-control", "private");
			response.CacheControl = "no-cache";

			response.Write(str);
			response.End();
		}

		/// <summary>过滤 超链接/javascript的关键字,比如" ' </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		public static string FixTitle(string str) {
			if (string.IsNullOrEmpty(str)) return "";

			string sRet = str;
			sRet = sRet.Replace("\"", "");
			sRet = sRet.Replace("'", "");
			sRet = sRet.Replace("<", "");
			sRet = sRet.Replace(">", "");
			return sRet;
		}

		/// <summary>清除给定字符串中的回车及换行符</summary>
		/// <param name="str">要清除的字符串</param>
		/// <returns>清除后返回的字符串</returns>
		public static string ClearCrLf(string str) {
			str = str.Replace("\r\n", "");
			str = str.Replace("\r", "");
			str = str.Replace("\n", "");
			return str;
		}

		/// <summary> 获得当前页面客户端的IP </summary>
		/// <returns>当前页面客户端的IP</returns>
		public static string GetIP() {
			string result = String.Empty;
			result = HttpContext.Current.Request.ServerVariables["HTTP_VIA"];
			if (string.IsNullOrEmpty(result)) {
				result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
			}
			if (string.IsNullOrEmpty(result)) {
				result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
			}
			if (string.IsNullOrEmpty(result)) {
				result = HttpContext.Current.Request.UserHostAddress;
			}
			if (string.IsNullOrEmpty(result)) {
				return "127.0.0.1";
			}
			return result;
		}

		/// <summary>获得Get提交的参数值,小于0返回0,否则返回int值</summary>
		/// <param name="strName">表单参数</param>
		/// <returns>返回 大于等于0的int型</returns>
		public static int GetId0(string strName) {
			if (HttpContext.Current.Request.QueryString[strName] == null) {
				return 0;
			}

			return ConvertHelper.Cint0(StringHelper.FilterSql(HttpContext.Current.Request.QueryString[strName]));
		}

	}
}
